	<meta charset="UTF-8">
<?php 
	
	include_once 'includes/init.php';
	include_once 'includes/upload.class.php';
	session_start();
	error_reporting(E_ERROR | E_PARSE);
	$nphone = isset($_SESSION['phone']) ? $_SESSION['phone'] : '';
	$mid=isset($_SESSION['id'])?$_SESSION['id']:'';
	$optype = empty($_FILES['excelfile']['tmp_name']) ? "0" : "1" ;
	$tarr = array(
	 		'filepath'=>'upload',
	 		'maxsize'=>'8000000000000',
	 		'allowmime'=>array(
				'image/jpeg',
	            'image/jpg',
	            'image/pjpeg',
	            'image/gif',
	            'image/png',
	            'image/x-png',
	            'application/msexcel',
	            'application/pdf',
	            'application/vnd.ms-excel',
	            'application/msword',
	            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
	            'text/plain',
	        ),
	        'israndname'=>'1',
	    );
	if($optype == 0){           //表单上传
		
			$upObj = new fileup($tarr); 
		 	$filename = $upObj->up('file_name');
		 	if ($filename == '') { // 若文件名为空则输出错误信息
		 		echo 'msg= '.$upObj->geterror();
		 	}else {
		 		$_POST['file_name'] = $filename;
		 	}
		 	// var_dump($filename);die;
		 	array_push($_POST['sid'], $mid);
		 	$sid = implode(",", $_POST['sid']);
		 	$lasttime = strtotime($_POST['lasttime']);

		 	for ($i=0; $i < count($_POST['type']); $i++) { 
		 		$sql = "insert into t_product 
(mid,sid,type,size,units,number,price,currency,status,mifeng,lasttime,note,nphone,file_name,project,price_type,material,purchase,plant,extranum,validtime,invalidtime,purchase_type)
 values
 ('".$mid."','".$sid."','".$_POST['type'][$i]."','".$_POST['size'][$i]."','".$_POST['units'][$i]."','".$_POST['number'][$i]."','".$_POST['price'][$i]."','".$_POST['currency'][$i]."','".$_POST['status']."','".$_POST['mifeng']."','".$lasttime."','".$_POST['note'][$i]."','".$nphone."','".$_POST['file_name'][$i]."','".$_POST['project']."','".$_POST['price_type'][$i]."','".$_POST['material'][$i]."','".$_POST['purchase'][$i]."','".$_POST['plant'][$i]."','".$_POST['extranum'][$i]."','".strtotime($_POST['validtime'])."','".strtotime($_POST['invalidtime'])."','".$_POST['purchase_type']."')";
				$result = $db_Obj->query($sql);
		 	}
		 	if($result == true){

				echo "<script>alert('添加成功');window.location.href='addpro.php';</script>";
		 	}
	}
	if($optype == 1){           //excel文件上传
		if(!empty($_FILES['excelfile']['tmp_name'])){
			$file_types = explode(".", $_FILES['excelfile']['name']);
			$file_type = $file_types[count($file_types) - 1];
		  	/*判别是不是.xls文件，判别是不是excel文件*/
	     	if (strtolower ( $file_type ) != "xls"){
	          	$this->error ( '不是Excel文件，重新上传' );
     		}
     		$upObj = new fileup($tarr); 
		 	$filename = $upObj->up('excelfile');
			global $mid;
			require_once "reader.php";
			$data = new Spreadsheet_Excel_Reader(); //创建读取Excel 对象
			$data->setOutputEncoding('utf-8');    //设置读取Excel内容后输出的字符编码
	    	$data->read("upload/{$filename}");
	    	// $datas = object_array($data);
	    	$a = $data->sheets[0]['numRows'];
	    	$b = $data->sheets[0]['numCols'];

	    	$sid = implode(",", $_POST['sid']);
	    	$project = $_POST['project'];
	    	$purchase_type = $_POST['purchase_type'];
	    	$lasttime = strtotime($_POST['lasttime']);
	    	$validtime = strtotime($_POST['validtime']);
	    	$invalidtime = strtotime($_POST['invalidtime']);
	    	$status = $_POST['status'];
	    	$mifeng = $_POST['mifeng'];

	    	for ($i=2; $i <= $a; $i++) { 
	    			$type = $data->sheets[0]['cells'][$i][1];
	    			$size = $data->sheets[0]['cells'][$i][2];
	    			$units = $data->sheets[0]['cells'][$i][3];
	    			$number = $data->sheets[0]['cells'][$i][4];
	    			$price = $data->sheets[0]['cells'][$i][5];
	    			$currency = $data->sheets[0]['cells'][$i][6];
	    			$price_type = $data->sheets[0]['cells'][$i][7];
	    			$note = $data->sheets[0]['cells'][$i][8];
	    			$material = $data->sheets[0]['cells'][$i][9];
	    			$purchase = $data->sheets[0]['cells'][$i][10];
	    			$plant = $data->sheets[0]['cells'][$i][11];
	    			$extranum = $data->sheets[0]['cells'][$i][12];

	    			$sql = "insert into t_product (mid, sid, type, size, number, price, lasttime, status, nphone, units, currency, project, mifeng, price_type, note, material, purchase, plant, extranum, validtime, invalidtime,purchase_type) values('".$mid."','".$sid."','".$type."','".$size."','".$number."','".$price."','".$lasttime."','".$status."','".$nphone."','".$units."','".$currency."','".$project."','".$mifeng."','".$price_type."','".$note."','".$material."','".$purchase."','".$plant."','".$extranum."','".$validtime."','".$invalidtime."','".$purchase_type."') ";
	    			// echo $sql;
	    			$result = $db_Obj->query($sql);
	    	}
	    	if($result == true){
				echo "<script>alert('添加成功');window.location.href='addpro.php';</script>";
		 	}
		}
	 	
	}


 ?>
